package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

        @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     *
     根据品牌id，查询商品分类的集合
     * @return
     */


    @Override
    public List<CategoryDTO> queryCategoryBrandbyId(Long id) {
        //根据品牌id查询中间表，获取中间表的对象集合
        List<CategoryBrand> brand_id = this.categoryBrandService.query().eq("brand_id", id).list();

        //从中间表的对象集合中获取分类id的集合
        List<Long> collect = brand_id.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
       // return listCategoryByIds;

        return listCategoryById(collect);
    }




    public List<CategoryDTO> listCategoryById(List<Long> ids) {

        return CategoryDTO.convertEntityList(listByIds(ids));
    }

    /**
     * 根据分类id，查询商品分类
     * @param id 分类id,路径携带参数的标准接参方式
     * @return 分类对象
     */
    @Override
    public CategoryDTO queryCategoryById(Long id) {

        return new CategoryDTO(this.getById(id));
    }

    /**
     * 根据分类的pid查询对应的分类集合，根据父类目id，查询子类目的集合
     * @param pid
     * @return
     */
    @Override
    public List<CategoryDTO> qureyParentByPid(Long pid) {
        List<Category> categories = query().eq("parent_id", pid).list();
        /**将查询到的po转换为DTo
//        List<CategoryDTO>categoryDTOS=new ArrayList<>();
//        for (Category category : categories) {
//            CategoryDTO categoryDTO = new CategoryDTO();
//            BeanUtils.copyProperties(category,categoryDTO);
//            categoryDTOS.add(categoryDTO);
//        }
*/
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);
        return categoryDTOS;




}


}